<?php

/**
 * Helper para formataçoes
 *
 * @author Aquiles B da Silva
 */
class Atendimento_View_Helper_Atend extends Zend_View_Helper_Abstract {

    public function atend() {
        return $this;
    }

    public function limpaReplace($str) {
        for ($index = 1; $index <= 10; $index++) {
            for ($index2 = 1; $index2 <= 10; $index2++) {
                $str = str_replace("#atedn" . $index . $index2, "", $str);
            }
            $str = str_replace("#func" . $index, "", $str);
        }

        return $str;
    }

    public function replaceAtend($atendFun,$replace) {
        if (count($atendFun) > 0) {
            $lastId = null;
            $str = "";
            $func = 0;
            $atd = 1;
            
            $replAtendimento = '<a class="consFunc">';
            foreach ($atendFun as $atend) {
                if ($lastId != $atend['funcionario']) {
                    $lastId = $atend['funcionario'];
                    $func++;
                    $atd = 1;
                    $replace = str_replace('#func' . $func, $this->replaceFunc($atend), $replace);
                    $replace = str_replace('#atedn' . $func . $atd, $this->replaceAtendStatus($atend), $replace);
                } else {
                    $atd++;
                    $replace = str_replace('#atedn' . $func . $atd, $this->replaceAtendStatus($atend), $replace);
                }
            }
        }
        
        return $this->limpaReplace($replace);
    }

    private function replaceFunc($atend){
        return '<a href="#" class="consFunc" idFunc="'.$atend['funcionario'].'">'.$atend['nome_funcionario'].'</a>';
    }

    private function replaceAtendStatus($atend){
        return '<a href="#" class="consAtn" idAtnd="'.$atend['codigo'].'">'.$atend['status'].'</a>';
    }
}